package ch7.ex;

import java.util.Random;

//取钱人
public class Taker extends Thread {
	private Accout ac;

	public Taker(String name, Accout ac) {
		super(name);
		this.ac = ac;
	}

	@Override
	public void run() {
		Random r = new Random();
		// int total = 0;
		while (true) {
			int money = r.nextInt(300);
			System.out.println("账户余额是：" + ac.getMoney() + "," + super.getName() + "准备取钱：" + money + "元");

			synchronized (ac) {
				while (this.ac.getMoney() < money) {
					System.out.println("余额不足，" + super.getName() + "等着。");
					try {
						ac.wait(5000);
					} catch (InterruptedException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}

				this.ac.withDraw(money);
				// total += money;
				System.out.println("当前余额是" + ac.getMoney() + "元，" + super.getName() + "取：" + money + "元");

			}
		}
	}
}
